// 发送sms按钮
var smsButton = Vue.extend({
    template: '<a href="javascript:;" v-on:click="send_passwd_sms" class="weui-vcode-btn">发送密码短信</a>',
    methods: {
        send_passwd_sms: function(){
            var self = this;
            var $div_mobile = $(this.$parent.$el).find('#div_mobile');
            $div_mobile.form();
            $div_mobile.validate(function(error){
                if(error){
                    //return true; // 如果return true的话，则不会显示错误提示
                }else{
                    var data = {mobile: self.$parent.mobile};
                    $.showLoading();
                    $.ajax({
                        type: 'POST',
                        url: '/card/m/send_sms_password',
                        data: data,
                        dataType: 'json',
                        timeout: 15000,
                        success: function (data) {
                            $.hideLoading();
                            if (data.errcode) {
                                if (data.errcode == 1002)
                                    $("#div_mobile").addClass("weui_cell_warn");
                                if (data.errlevel == 'forbidden')
                                    $.toast(data.errmsg, "forbidden");
                                else
                                    $.toptips(data.errmsg);
                            }
                            else if (data.result == 'OK'){
                                self.$parent.sms_button = 'sms-sending';
                            }
                        },
                        error: function (xhr, type) {
                            $.toptips('网络有问题,稍后再试');
                            $.hideLoading();
                        }
                    });

                }
            });
        },
    }
});

// 发送完成提示
var smsSending = Vue.extend({
    created: function () {
        this.count_down();
    },
    data: function(){
        // 如果有剩余倒计时
        if (count_down_seconds > 0){
            count = count_down_seconds;
        }
        else{
            count = 61;
        }
        return {
            count: count
        }
    },
    template: '<span class="weui-vcode-btn">({{count}})</span>',
    methods: {
        count_down: function(){
            this.count --;
            if (this.count > 0)
                setTimeout(this.count_down, 1000);
            else{
                count_down_seconds = 0;
                this.$parent.sms_button = 'sms-button';
            }
        }
    }
});

// 2.注册组件，并指定组件的标签，组件的HTML标签为<my-component>
Vue.component('sms-button', smsButton);
Vue.component('sms-sending', smsSending);


var data = {
    mobile: '',
    password: '',
    sms_button: 'sms-button',
    validate_code: null
};

if (window.count_down_seconds > 0){
    data['sms_button'] = 'sms-sending';
}

// 校验手机号码、提货密码表单
new Vue({
    el: '#form-mobile-passwd',
    data: data,
    methods: {
        validate_code_reload: function(e){
            var d = new Date();
            $('#img_validate_code').attr('src', '/validate_code?' + d.getTime());
        },
        submit: function(){
            var self = this;
            var $form = $(this.$el);
            //表单校验
            $form.form();
            $form.validate(function(error){
                if(error){
                    //return true; // 如果return true的话，则不会显示错误提示
                }else{
                    var data = $form.serializeArray();
                    $.showLoading();
                    $.ajax({
                        type: 'POST',
                        url: '/card/m/validate/password',
                        data: data,
                        dataType: 'json',
                        timeout: 15000,
                        success: function (data) {
                            $.hideLoading();
                            if (data.errcode){
                                if (data.errcode == 1001)
                                    $("#div_validate_code").addClass("weui_cell_warn");
                                if (data.errcode == 1002)
                                    $("#div_mobile").addClass("weui_cell_warn");
                                if (data.errcode == 1004)
                                    $("#div_password").addClass("weui_cell_warn");
                                $.toptips(data.errmsg);
                            }
                            else if (data.result == 'OK'){
                                if (data.is_exchanged){
                                    window.location.href = '/card/m/info'
                                }
                                else{
                                    window.location.href = '/card/m/new'
                                }
                            }
                        },
                        error: function (xhr, type) {
                            $.toptips('网络有问题,稍后再试');
                            $.hideLoading();
                        }
                    });
                }
            });
        }
    }
});

// 新的提货记录表单
new Vue({
    el: '#form-new',
    data: {},
    methods: {
        submit: function(){
            var $form = $(this.$el);
            //表单校验
            $form.form();
            $form.validate(function(error){
                if(error){
                    //return true; // 如果return true的话，则不会显示错误提示
                }else{
                    var data = $form.serializeArray();
                    $.showLoading();
                    $.ajax({
                        type: 'POST',
                        url: '/card/m/save',
                        data: data,
                        dataType: 'json',
                        timeout: 15000,
                        success: function (data) {
                            $.hideLoading();
                            if (data.errcode){
                                $.toptips(data.errmsg);
                                //已兑换
                                if (data.errcode == 2003){
                                    window.location.href = '/card/m/info'
                                }
                            }
                            else if (data.result == 'OK'){
                                window.location.href = '/card/m/info'
                            }
                        },
                        error: function (xhr, type) {
                            $.toptips('网络有问题,稍后再试');
                            $.hideLoading();
                        }
                    });
                }
            });
        }
    }
});

$(function(){
    if (window.error){
        $.toast(window.error, "forbidden");
    }
    else{
        $("#ssx").cityPicker({
            title: "选择省市县"
        });
    }
});
